<?php


namespace App\Http\Controller\Admin;

use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use App\Model\Logic\CourseLogic;

/**
 * Class CourseController
 * @Controller(prefix="/admin")
 * @package App\Http\Controller\Admin
 */
class CourseController
{
    /**
     * @Inject()
     * @var CourseLogic
     */
    private $courselogic;

    /**
     * @RequestMapping(route="course/list", method={RequestMethod::GET})
     * @return \Swoft\Http\Message\Response
     * @throws \Throwable
     */
    public function index(){
        return view("admin/course/list");
    }

    /**
     * @RequestMapping("course/getdata")
     * @param Request $request
     * @return \Swoft\Http\Message\Response
     */
    public function getData(Request $request){
        $data = $request->get();
        $data = $this->courselogic->getDatas($data);
        return context()->getResponse()->withData($data);
    }

    /**
     * @RequestMapping(route="course/create", method={RequestMethod::GET})
     * @return \Swoft\Http\Message\Response
     * @throws \Throwable
     */
    public function create(){
        return view("admin/course/create");
    }

    /**
     * @RequestMapping(route="course/save")
     * @return \Swoft\Http\Message\Response
     * @throws \Throwable
     */
    public function save(Request $request){
        //获取数据
        $data = $request->post();
        //提交到logic
        $this->courselogic->addData($data);
    }

    /**
     * 设置程是否推荐
     * @RequestMapping("course/setshow")
     */
    public function setIsShow(Request $request){
        $data = $request->post();
        $this->courselogic->setIsShow($data);
    }

    /**
     * @RequestMapping("course/edit/{id}")
     * @return string
     * @throws \Throwable
     */
    public function edit(int $id){
        //获取课程信息
        $course = $this->courselogic->getCourseFromId($id);
        $data = [
            "course"=>$course
        ];
        return view("admin/course/edit",$data);
    }

    /**
     * 接收编辑数据并提交到逻辑层处理
     * @RequestMapping("course/update/{id}")
     * @param Request $request
     * @param int $id
     * @throws \App\Exception\HttpException
     * @throws \ReflectionException
     * @throws \Swoft\Bean\Exception\ContainerException
     */
    public function update(Request $request,int $id){
        //获取数据
        $data = $request->post();
        $datas = [
            "id"=>$id,
            "data"=>$data
        ];
        //提交到logic
        $this->courselogic->updateData($datas);
    }

    /**
     * 删除接口
     * @RequestMapping(route="course/del")
     * @param Request $request
     * @throws \ReflectionException
     * @throws \Swoft\Bean\Exception\ContainerException
     */
    public function del(Request $request){
        $ids = $request->post("ids");
        $this->courselogic->del($ids);
    }
}
